package com.yunfeisoft.base.controller;

import com.yunfeisoft.base.model.WorkflowStep;
import com.yunfeisoft.base.model.WorkflowStepUser;
import com.yunfeisoft.base.service.inter.WorkflowStepService;
import com.yunfeisoft.base.service.inter.WorkflowStepUserService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.FastJsonUtils;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * ClassName: WorkflowStepController
 * Description: 流程步骤定义Controller
 * Author: Jackie liu
 * Date: 2020-01-03
 */
@RestController
public class WorkflowStepController extends BaseController {

    @Autowired
    private WorkflowStepService workflowStepService;
    @Autowired
    private WorkflowStepUserService workflowStepUserService;

    /**
     * 添加流程步骤定义
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowStep/save", method = RequestMethod.POST)
    public Response save(HttpServletRequest request, HttpServletResponse response) {
        String items = ServletRequestUtils.getStringParameter(request, "items", null);
        if (StringUtils.isBlank(items)) {
            return ResponseUtils.warn("没有流程步骤信息，无法保存");
        }

        List<WorkflowStep> workflowStepList = FastJsonUtils.jsonToList(items, WorkflowStep.class);
        if (CollectionUtils.isEmpty(workflowStepList)) {
            return ResponseUtils.warn("没有流程步骤信息，无法保存");
        }
        workflowStepService.batchSave(workflowStepList);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改流程步骤定义
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    /*@RequestMapping(value = "/web/workflowStep/modify", method = RequestMethod.POST)
    public Response modify(WorkflowStep record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        workflowStepService.modify(record);
        return ResponseUtils.success("保存成功");
    }*/

    /**
     * 查询流程步骤定义
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowStep/query", method = {RequestMethod.POST, RequestMethod.GET})
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        WorkflowStep record = workflowStepService.load(id);
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询流程步骤定义
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowStep/list", method = {RequestMethod.POST, RequestMethod.GET})
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String workflowDefineId = ServletRequestUtils.getStringParameter(request, "workflowDefineId", null);

        List<WorkflowStep> list = workflowStepService.queryByWorkflowDefineId(workflowDefineId);
        for (WorkflowStep workflowStep : list) {
            List<WorkflowStepUser> userList = workflowStepUserService.queryByWorkflowStepId(workflowStep.getId());
            workflowStep.setUserList(userList);
        }
        return ResponseUtils.success(list);
    }

    /**
     * 批量删除流程步骤定义
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowStep/delete", method = {RequestMethod.POST, RequestMethod.GET})
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        workflowStepService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }

    /**
     * 获取下一个步骤执行人
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/workflowStep/getNextStepUsers", method = {RequestMethod.POST, RequestMethod.GET})
    public Response getNextStepUsers(HttpServletRequest request, HttpServletResponse response) {
        String workflowDefineId = ServletRequestUtils.getStringParameter(request, "workflowDefineId", null);
        int stepNum = ServletRequestUtils.getIntParameter(request, "stepNum", 0);

        List<WorkflowStepUser> stepUsers = workflowStepService.getNextStepExecutor(workflowDefineId, stepNum);
        return ResponseUtils.success(stepUsers);
    }
}
